{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Animation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       " <svg><polygon fill=\"yellow\" stroke=\"none\"\n",
       "    points=\"350 75, 379 161, 469 161, 397 215,\n",
       "            423 301, 350 250, 277 301, 303 215,\n",
       "            231 161, 321 161\"\n",
       "    transform=\"scale(0.5000000000000003)\" /></svg> "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "namespace import ::jupyter::*\n",
    "set size 0.1\n",
    "set svg {\n",
    " <svg><polygon fill=\"yellow\" stroke=\"none\"\n",
    "    points=\"350 75, 379 161, 469 161, 397 215,\n",
    "            423 301, 350 250, 277 301, 303 215,\n",
    "            231 161, 321 161\"\n",
    "    transform=\"scale(@@)\" /></svg> }\n",
    "set id [ html [string map [list @@ $size ] $svg ]]\n",
    "while {1} {\n",
    "  after 5\n",
    "  set size [expr {$size+0.005}]\n",
    "    updatehtml $id [string map [list @@ $size ] $svg ]\n",
    "    if {$size > 0.5} return\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Countdown"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tcl events are fully supported. In combination with the `update` commands we can use this for timed animation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<b>0</b>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "set count 11\n",
    "set disp [jupyter::html \"<b>$count</b>\"]\n",
    "proc loop {id} {\n",
    "    incr ::count -1\n",
    "    jupyter::updatehtml $id \"<b>$::count</b>\"\n",
    "    if {$::count > 0} {after 1000 \"loop $id\"}\n",
    "    return\n",
    "}\n",
    "loop $disp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plantuml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "  package require base64\n",
    "\n",
    "  proc plantumlurl {raw} {\n",
    "    return https://www.plantuml.com/plantuml/svg/[string map [list \\n {}] [string trim [base64toplantuml [base64::encode [zlib deflate $raw]]]]]\n",
    "  }\n",
    " \n",
    "  variable base64map\n",
    "  foreach x {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /}\\\n",
    "    y {0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z - _} {\n",
    "    lappend base64map $x $y\n",
    "  }\n",
    "    interp alias {} [namespace current]::base64toplantuml {} string map $base64map \n",
    "    proc plantuml {raw} {jupyter::html \"<p><img src='[plantumlurl $raw]'/>\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p><img src='https://www.plantuml.com/plantuml/svg/TOwz2eD048JxVOh5tXUGaCRgi3nB8IpU4Wykw-Ns5FBsYJ-OMAIT-UPZ42Abx7iVIJ8MTV0ikHFBgdwK0acnjcUcuIGSrgevD_kgatP8J6v5RFk7CP8vnOqn0BN5ihnYWnKcVawi6Q1P8zjME92umBU7ti6sk8IcGfuf0FNPpvHvaLXp39pN6EYrqelscspIslum1lTLOfr21iSdww7Owy_LIrdkLpMEeWnl'/>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "display-id-3\r\n"
     ]
    }
   ],
   "source": [
    "plantuml { \n",
    "participant \"Client\\nSession\" as CS\n",
    "participant \"Kernel\" as K\n",
    "participant \"Session\\nThread\" as ST\n",
    "participant \"Session\\nInterp\" as SI\n",
    "\n",
    "CS --> K : request\n",
    "\n",
    "K --> ST: handle_msg\n",
    "ST -> SI: eval\n",
    "activate SI\n",
    "SI --> ST: display\n",
    "ST --> K: display\n",
    "K --> CS: display\n",
    "SI -> ST: result\n",
    "deactivate SI\n",
    "ST --> K: result\n",
    "CS <-- K : response\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Tcl 8.7",
   "language": "tcl",
   "name": "tcl87"
  },
  "language_info": {
   "file_extension": ".tcl",
   "mimetype": "txt/x-tcl",
   "name": "tcl",
   "version": "8.7a3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
